<template>
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      class="avatar"
      round
      fit="cover"
      :src="data.aut_photo"
    />
    <div
      slot="title"
      class="title-wrap"
    >
      <div class="user-name">{{data.aut_name}}</div>
    </div>
    <van-button
      @click="onLike"
      :class="{like:data.is_liking}"
      class="like-btn"
      :icon="data.is_liking?'good-job':'good-job-o'"
    >{{data.like_count || '赞'}}</van-button>
    <div slot="label">
      <p class="comment-content">{{data.content}}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{data.pubdate | relativeTime}}</span>
        <van-button
          @click="$emit('re-comment',data)"
          class="reply-btn"
          round
        >回复 {{data.reply_count}}</van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addCommentsLik, deleteCommentsLike } from '@/api/comment.js'
export default {
  name: 'CommentItem',
  components: {},
  props: {
    data: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      commentShow: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async  onLike () {
      try {
        if (this.data.is_like === false) {
          await addCommentsLik(this.data.com_id)
        } else {
          await deleteCommentsLike(this.data.com_id)
        }
        this.data.is_liking = !this.data.is_liking
        this.data.like_count = this.data.is_liking ? this.data.like_count + 1 : this.data.like_count - 1
      } catch (err) {
        if (err.response && err.response.status === 401) {
          return this.$toast('请先登录')
        }
        this.$toast('操作失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
  }
}
.like {
  color: rgb(186, 81, 81);
}
</style>
